package hw.txtreader.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import hw.txtreader.application.MyApplication;


public class SPUtil {

    private static final String SHARE_PREFS_NAME = "hwtxt_reader";

    public static void putBoolean(Context context,String key, boolean value) {
        SharedPreferences pref = context.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        pref.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key,
                                     boolean defaultValue) {
        SharedPreferences pref = MyApplication.getInstance().getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        return pref.getBoolean(key, defaultValue);
    }

    /**
     * 保存时 值
     * 非加密
     */
    public static void putString(Context context,String key, String value) {
        SharedPreferences pref = context.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);
        pref.edit().putString(key, TextUtils.isEmpty(value) ? "" : value).commit();
    }

    /**
     * 获取时 值
     * 非加密
     */
    public static String getString(Context context,String key, String defaultValue) {
        SharedPreferences pref = context.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);
        return pref.getString(key, defaultValue);
    }

    public static void putInt(String key, int value) {
        SharedPreferences pref = MyApplication.getInstance().getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        pref.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defaultValue) {
        SharedPreferences pref = MyApplication.getInstance().getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        return pref.getInt(key, defaultValue);
    }
}
